package action;

import global.Constants;
import global.beanUtils.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import service.TodoCategoryService;
import util.ServletHelp;
import util.StringUtils;
import domain.TodoCategory;

/**
 * 任务分类Action类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class TodoCategoryAction extends BaseAction {
	/**
	 * 任务分类Service
	 * */
	private TodoCategoryService todoCategoryService = null;

	/**
	 * 设定任务分类Service
	 * @param todoCategoryService 任务分类Service
	 */
	public void setTodoCategoryService(TodoCategoryService todoCategoryService) {
		this.todoCategoryService = todoCategoryService;
	}
	
	/**
	 * 任务分类查询方法
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward query(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// 参数
		TodoCategory paramTodoCategory = new TodoCategory();
		// 取得收支项目列表
		List<TodoCategory> todoCategoryList = new ArrayList<TodoCategory>();
		JSONArray datas = new JSONArray();
		todoCategoryList = todoCategoryService.selectByCriteria(paramTodoCategory);
		if(null!=todoCategoryList){
			datas = JSONArray.fromObject(todoCategoryList);
		}

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("datas", datas);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
	
	/**
	 * 获得任务分类树
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward getTodoCategoryTree(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 取得任务分类列表
		List<TodoCategory> todoCategoryList = new ArrayList<TodoCategory>();
		TodoCategory paramTodoCategory = new TodoCategory();
		todoCategoryList = todoCategoryService.selectByCriteria(paramTodoCategory);
		JSONArray res = getTodoCategoryTreeFromList(todoCategoryList);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 保存任务分类
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	@SuppressWarnings("unchecked")
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 参数设定
		Map map = request.getParameterMap();
		TodoCategory todoCategory = new TodoCategory();
		BeanUtils.populate(todoCategory, map);
		todoCategory.setStatus(Constants.STATUS_1);
		todoCategory.setDelflag(Constants.DEL_FLAG_1);

		String categoryId = request.getParameter("categoryId");
		if(StringUtils.isNotEmpty(categoryId)){
			// 更新任务分类
			todoCategoryService.update(todoCategory);
		}else{
			// 添加任务分类
			todoCategoryService.insert(todoCategory);
		}

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 删除任务分类
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 参数任务分类ID
		String categoryId = request.getParameter("categoryId");
		if (StringUtils.isNotEmpty(categoryId)) {
			// 删除任务分类
			todoCategoryService.delete(Integer.parseInt(categoryId));
		}
		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 返回账目树
	 * @param todoCategoryList
	 * @return
	 */
	private JSONArray getTodoCategoryTreeFromList(List<TodoCategory> todoCategoryList) {
		JSONArray results = new JSONArray();
		// 所有任务
		JSONObject allTodo = new JSONObject();
		allTodo.put("id", "allTodo");
		allTodo.put("text", "所有任务");
		allTodo.put("leaf", true);
		results.add(allTodo);
		// 未完成任务
		JSONObject uncompleteTodo = new JSONObject();
		uncompleteTodo.put("id", "uncompleteTodo");
		uncompleteTodo.put("text", "未完成任务");
		uncompleteTodo.put("leaf", true);
		results.add(uncompleteTodo);
		// 已完成任务
		JSONObject completeTodo = new JSONObject();
		completeTodo.put("id", "completeTodo");
		completeTodo.put("text", "已完成任务");
		completeTodo.put("leaf", true);
		results.add(completeTodo);
		// 任务分类
		if (null != todoCategoryList) {
			JSONArray children = new JSONArray();
			TodoCategory temp = new TodoCategory();
			for (int i = 0; i < todoCategoryList.size(); i++) {
				temp = todoCategoryList.get(i);
				JSONObject todoCategoryNode = new JSONObject();
				todoCategoryNode.put("id", temp.getCategoryId());
				todoCategoryNode.put("text", temp.getCategoryName());
				todoCategoryNode.put("leaf", true);
				children.add(todoCategoryNode);
			}
			JSONObject todoCategory = new JSONObject();
			todoCategory.put("id", "0");
			todoCategory.put("text", "任务分类");
			todoCategory.put("children", children);
			results.add(todoCategory);
		}
		return results;
	}
}
